package com.googlecode.maurersmarbles;

import java.util.ArrayList;
import java.util.List;

public class Play {

	/**
	 * The card for this play.
	 */
	private Card card;
	/**
	 * The list of moves for this play.
	 */
	private List<Move> moves = new ArrayList<Move>();

	/**
	 * Creates a new Play.
	 * @param card the card for this play
	 */
	public Play(Card card) {
		this.card = card;
	}

	/**
	 * Returns the card for this play.
	 * @return the card for this play
	 */
	public Card getCard() {
		return card;
	}

	/**
	 * Adds the move to this play.
	 * @param move the move to add
	 */
	public void addMove(Move move) {
		moves.add(move);
	}

	/**
	 * Adds the list of moves to this play.
	 * @param moves the list of moves to add
	 */
	public void addMoves(List<Move> moves) {
		this.moves.addAll(moves);
	}

	/**
	 * Returns the moves for this play.
	 * @return the moves for this play
	 */
	public List<Move> getMoves() {
		return moves;
	}
		
	/**
	 * Returns whether or not the given player number and marble number are 
	 * the same as this play.
	 * @param playerNumber1 the player number
	 * @param marbleNumber1 the marble number
	 * @return whether or not the given player number and marble number are
	 * the same as this play
	 */
	public boolean isSamePlay(int playerNumber, int marbleNumber) {
		if (moves.isEmpty()) {
			return false;
		}
		if (moves.get(0).isSamePlayerAndMarble(playerNumber, marbleNumber)) {
			return true;
		}
		return false;
	}
	
	/**
	 * Returns whether or not the given player numbers and marble numbers are 
	 * the same as this play.
	 * @param playerNumber1 the first player number
	 * @param marbleNumber1 the first marble number
	 * @param playerNumber2 the second player number
	 * @param marbleNumber2 the second marble number
	 * @return whether or not the given player numbers and marble numbers are
	 * the same as this play
	 */
	public boolean isSamePlay(int playerNumber1, int marbleNumber1,
			int playerNumber2, int marbleNumber2) {
		if (moves.size() < 2) {
			return false;
		}
		if (moves.get(0).isSamePlayerAndMarble(playerNumber1, marbleNumber1)
				&& moves.get(1).isSamePlayerAndMarble(playerNumber2,
						marbleNumber2)) {
			return true;
		}
		else if (moves.get(1).isSamePlayerAndMarble(playerNumber1,
				marbleNumber1)
				&& moves.get(0).isSamePlayerAndMarble(playerNumber2,
						marbleNumber2)) {
			return true;
		}
		return false;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((card == null) ? 0 : card.hashCode());
		result = prime * result + ((moves == null) ? 0 : moves.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		Play other = (Play) obj;
		if (card == null) {
			if (other.card != null) {
				return false;
			}
		}
		else if (!card.equals(other.card)) {
			return false;
		}
		if (moves == null) {
			if (other.moves != null) {
				return false;
			}
		}
		else if (!moves.equals(other.moves)) {
			return false;
		}
		return true;
	}
	
}
